import 'package:app_health/constant/Constans.dart';
import 'package:app_health/pages/login/LoginHomePage.dart';
import 'package:app_health/main.dart';
import 'package:app_health/pages/login/LoginPage.dart';
import 'package:app_health/pages/regist/RegistPage.dart';
import 'package:app_health/utils/ImageUtils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 引导页
class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  var guideList = [
    ImageUtils.getImagePath('guide_img_1', format: 'jpg'),
    ImageUtils.getImagePath('guide_img_2', format: 'jpg'),
    ImageUtils.getImagePath('guide_img_3', format: 'jpg'),
  ];

  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          Swiper(
            itemCount: guideList.length,
            // 无限轮播
            loop: false,
            itemBuilder: (BuildContext context, int position) {
              return Image.asset(
                guideList[position],
                fit: BoxFit.fitHeight,
              );
            },
            onIndexChanged: (int index) {
              _currentIndex = index;
              print('[current index = $_currentIndex]');
              // 刷新界面
              setState(() {});
            },
          ),
          Offstage(
            // offstage =true 则隐藏 false则显示 故等于2的时候显示
            offstage: !(_currentIndex == 2),
            child: GestureDetector(
              onTap: () {
                print('click bottom');
                startMyAppPage();
              },
              child: Container(
                margin: EdgeInsets.only(left: 50.0, right: 50.0, bottom: 20.0),
                width: double.infinity,
                height: 100,
                color: Colors.transparent,
                alignment: Alignment.bottomCenter,
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// 跳转了注册页面
  void startMyAppPage() {
    //_changeFirstLauncher(false);
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (context) => new LoginHomePage()));
  }

  /// 最好异步
  _changeFirstLauncher(bool value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool(Constants.FIRST_LAUNCHER, value);
  }
}
